<template>
  <div class="relationship-list">
    <CPageTable @register="registerPage" v-model:searchForm="searchForm">
      <!-- 排放源/目录 - Slot -->
      <template #directory="{ scope }">
        <CTableColumn :main-text="scope.row.sourceName">
          <template #subText>
            <span>{{ scope.row.frameworkDesc }}</span>
          </template>
        </CTableColumn>
      </template>
      <!-- 燃料/能源/填充物 - Slot -->
      <template #material="{ scope }">
        <CTableColumn
          :main-text="scope.row.materialName"
          :sub-text="`单位：${scope.row.dataUnit}`"
        ></CTableColumn>
      </template>
      <template #relationNo="{ scope }">
        <p v-ellTooltip:2 class="relationNo">{{ scope.row.relationNo }}</p>
      </template>

      <!-- ISO 14064 (因子) - Slot -->
      <template #ISO14064="{ scope }">
        <span v-if="scope.row.factorList[0]?.factorNo == '-1'">该标准无因子匹配</span>
        <template v-else>
          <CTableColumn>
            <template #main>
              <el-popover placement="bottom" :width="300">
                <template #reference>
                  <p class="g-link" style="color: #435970"
                    >{{ scope.row.factorList[0]?.factorName }}
                  </p>
                </template>
                <popContent :rows="scope.row" :index="0" />
              </el-popover>
            </template>
            <template #subText> 单位：{{ scope.row.factorList[0]?.factorUnit }} </template>
          </CTableColumn>
        </template>
      </template>
      <!-- GB/T 32150 (因子) - Slot -->
      <template #GBT32150="{ scope }">
        <span v-if="scope.row.factorList[1]?.factorNo == '-1'">该标准无因子匹配</span>
        <template v-else>
          <CTableColumn>
            <template #main>
              <el-popover placement="bottom" :width="300">
                <template #reference>
                  <p class="g-link" style="color: #435970">{{
                    scope.row.factorList[1]?.factorName
                  }}</p>
                </template>
                <popContent :rows="scope.row" :index="1" />
              </el-popover>
            </template>
            <template #subText> 单位：{{ scope.row.factorList[1]?.factorUnit }} </template>
          </CTableColumn>
        </template>
      </template>
      <!-- GHG Protocol (因子) - Slot -->
      <template #GHGProtocol="{ scope }">
        <span v-if="scope.row.factorList[2]?.factorNo == '-1'">该标准无因子匹配</span>
        <template v-else>
          <CTableColumn>
            <template #main>
              <el-popover placement="bottom" :width="300">
                <template #reference>
                  <p class="g-link" style="color: #435970"
                    >{{ scope.row.factorList[2]?.factorName }}
                  </p>
                </template>
                <popContent :rows="scope.row" :index="2" />
              </el-popover>
            </template>
            <template #subText> 单位：{{ scope.row.factorList[2]?.factorUnit }} </template>
          </CTableColumn>
        </template>
      </template>
      <!-- 操作列 - Slot -->
      <template #op="{ scope }">
        <CTableButtonGroup :buttons-config="getTableButtonsConfig(scope.row)" />
      </template>
    </CPageTable>
  </div>
  <RelationshipDialog
    v-model:visible="dialogVisible"
    :frameworkNo="frameworkNo"
    :dataInfo="dataInfo"
    :treeData="treeData"
    @refresh="refresh"
  />
</template>

<script lang="ts">
export default {
  name: 'RelationshipList'
};
</script>
<script lang="ts" setup>
import { CPageTable, useCPageTable, CPageTableProps } from '@/components/CPageTable';

import { defineProps, reactive, ref, watch, defineExpose } from 'vue';
// import { empty } from '@/utils/index';
import { useRelationshipList } from './useRelationshipList';
import RelationshipDialog from './RelationshipDialog/index.vue';
import { useMessage } from '@/hooks/useMessage';
import { delRelation } from '@/apis/system/config/relationship';
import { CTableColumn, CTableButtonGroup } from '@/components/CTable';
import { FactorSourceTypeMap } from '@/constants/enum';
import type { TableButtonItem } from '@/components/CTable';
import popContent from './popContent/index.vue';

const props = defineProps({
  frameworkNo: {
    type: String,
    default: ''
  },
  frameworkName: {
    type: String,
    default: ''
  },
  isParentNode: {
    type: Boolean,
    default: false
  },
  treeData: {
    type: Object,
    default: () => ({})
  }
});
const { createConfirm, createMessage } = useMessage();
const { searchOptions, searchForm, tableOptions, tableFieldsSetting } = useRelationshipList();
const options = reactive<CPageTableProps>({
  title: '关联关系列表',
  searchOptions: searchOptions,
  enableFieldsSetting: true,
  tableOptions,
  showPageHeader: false,
  tableFieldsSetting
});
const cascader = ref('');
const cascaderProps = {
  emitPath: false
};
const optionss = [
  {
    value: 'guide',
    label: 'Guide',
    children: [
      {
        value: 'disciplines',
        label: 'Disciplines',
        children: [
          {
            value: 'consistency',
            label: 'Consistency'
          },
          {
            value: 'feedback',
            label: 'Feedback'
          },
          {
            value: 'efficiency',
            label: 'Efficiency'
          },
          {
            value: 'controllability',
            label: 'Controllability'
          }
        ]
      },
      {
        value: 'navigation',
        label: 'Navigation',
        children: [
          {
            value: 'side nav',
            label: 'Side Navigation'
          },
          {
            value: 'top nav',
            label: 'Top Navigation'
          }
        ]
      }
    ]
  },
  {
    value: 'component',
    label: 'Component',
    children: [
      {
        value: 'basic',
        label: 'Basic',
        children: [
          {
            value: 'layout',
            label: 'Layout'
          },
          {
            value: 'color',
            label: 'Color'
          },
          {
            value: 'typography',
            label: 'Typography'
          },
          {
            value: 'icon',
            label: 'Icon'
          },
          {
            value: 'button',
            label: 'Button'
          }
        ]
      },
      {
        value: 'form',
        label: 'Form',
        children: [
          {
            value: 'radio',
            label: 'Radio'
          },
          {
            value: 'checkbox',
            label: 'Checkbox'
          },
          {
            value: 'input',
            label: 'Input'
          },
          {
            value: 'input-number',
            label: 'InputNumber'
          },
          {
            value: 'select',
            label: 'Select'
          },
          {
            value: 'cascader',
            label: 'Cascader'
          },
          {
            value: 'switch',
            label: 'Switch'
          },
          {
            value: 'slider',
            label: 'Slider'
          },
          {
            value: 'time-picker',
            label: 'TimePicker'
          },
          {
            value: 'date-picker',
            label: 'DatePicker'
          },
          {
            value: 'datetime-picker',
            label: 'DateTimePicker'
          },
          {
            value: 'upload',
            label: 'Upload'
          },
          {
            value: 'rate',
            label: 'Rate'
          },
          {
            value: 'form',
            label: 'Form'
          }
        ]
      },
      {
        value: 'data',
        label: 'Data',
        children: [
          {
            value: 'table',
            label: 'Table'
          },
          {
            value: 'tag',
            label: 'Tag'
          },
          {
            value: 'progress',
            label: 'Progress'
          },
          {
            value: 'tree',
            label: 'Tree'
          },
          {
            value: 'pagination',
            label: 'Pagination'
          },
          {
            value: 'badge',
            label: 'Badge'
          }
        ]
      },
      {
        value: 'notice',
        label: 'Notice',
        children: [
          {
            value: 'alert',
            label: 'Alert'
          },
          {
            value: 'loading',
            label: 'Loading'
          },
          {
            value: 'message',
            label: 'Message'
          },
          {
            value: 'message-box',
            label: 'MessageBox'
          },
          {
            value: 'notification',
            label: 'Notification'
          }
        ]
      },
      {
        value: 'navigation',
        label: 'Navigation',
        children: [
          {
            value: 'menu',
            label: 'Menu'
          },
          {
            value: 'tabs',
            label: 'Tabs'
          },
          {
            value: 'breadcrumb',
            label: 'Breadcrumb'
          },
          {
            value: 'dropdown',
            label: 'Dropdown'
          },
          {
            value: 'steps',
            label: 'Steps'
          }
        ]
      },
      {
        value: 'others',
        label: 'Others',
        children: [
          {
            value: 'dialog',
            label: 'Dialog'
          },
          {
            value: 'tooltip',
            label: 'Tooltip'
          },
          {
            value: 'popover',
            label: 'Popover'
          },
          {
            value: 'card',
            label: 'Card'
          },
          {
            value: 'carousel',
            label: 'Carousel'
          },
          {
            value: 'collapse',
            label: 'Collapse'
          }
        ]
      }
    ]
  },
  {
    value: 'resource',
    label: 'Resource',
    children: [
      {
        value: 'axure',
        label: 'Axure Components'
      },
      {
        value: 'sketch',
        label: 'Sketch Templates'
      },
      {
        value: 'docs',
        label: 'Design Documentation'
      }
    ]
  }
];
// const [registerPage, { refresh }] = usePageTable(options);
const [registerPage, { refresh }] = useCPageTable(options);

const dialogVisible = ref(false);
const addHandle = () => {
  dialogVisible.value = true;
  dataInfo.value = {};
};
const renderStandard = (text: string | number, type: string, factorUnit: string) => {
  let typeStr = type;
  if (type == 'materialNo') {
    typeStr = `(${type})`;
  }
  const unitArr = factorUnit.split('/');
  let unitBefore = unitArr[0] ?? 'kg';
  let unitAfter = unitArr[1] ?? 'kg';
  if (text || text === 0) {
    return text + `${unitBefore}${typeStr}/${unitAfter}`;
  }
  return `-`;
};
watch(
  () => props.frameworkNo,
  value => {
    searchForm.frameworkNo = value;
    searchForm.sourceNo = '';
    searchForm.materialNo = '';
    options.title = `${props.frameworkName}-关联关系列表`;
    refresh(true);
    console.log('isParentNode', props.isParentNode);
  }
);
const dataInfo = ref({});
const handleEdit = (row: any) => {
  dataInfo.value = row;
  dialogVisible.value = true;
};
const handleDel = (row: any) => {
  createConfirm('确定删除该数据吗？', '删除后需要重新添加！', {
    iconType: 'warning'
  }).then(async () => {
    await delRelation(row.relationNo);
    createMessage.success(`删除成功！`);
    refresh();
  });
};
defineExpose({ addHandle });
/**
 * 配置表格的按钮
 * @param row 表格行数据
 */
function getTableButtonsConfig(row: any): TableButtonItem[] {
  return [
    {
      text: '编辑',
      btnPermission: 'ECI_RELATIONSHIP_EDIT',
      onClick: () => handleEdit(row)
    },
    {
      text: '删除',
      btnPermission: 'ECI_RELATIONSHIP_DELETE',
      onClick: () => handleDel(row)
    }
  ];
}
</script>
<style scoped lang="scss">
.relationship-list {
  // background: #f3f7fb;
  .c-page-table__body {
    margin: unset;
  }
}
.popContent {
  p.g-font-12n2 {
    line-height: 22px;
    .title {
      display: inline-block;
      width: 78px;
    }
  }
}
.relationNo {
  line-height: 20px;
}
</style>
